package cn.cj.erp.common.security.security.utils;

import cn.cj.erp.common.constants.HttpStatus;
import cn.cj.erp.common.exception.ServiceException;
import cn.cj.erp.common.security.security.config.Sm3PasswordEncoder;
import cn.cj.erp.common.security.security.user.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 安全服务工具类
 *
 * @author ruoyi
 */
@Slf4j
public class SecurityUtils {

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        try {
            if (null != getAuthentication()) {
                Object principal = getAuthentication().getPrincipal();
                if (principal instanceof String && principal.toString().equals("anonymousUser")) {
                    //匿名用户
                    return new LoginUser();
                }
                if (!(principal instanceof LoginUser)) {
                    log.info("登录用户类型【{}】非{}", principal.getClass().getName(), LoginUser.class.getName());
                    throw new ServiceException(HttpStatus.UNAUTHORIZED, String.format("登录用户类型【%s】非%s", principal.getClass().getName(), LoginUser.class.getName()));
                }
                return (LoginUser) principal;
            }
            return new LoginUser();
        } catch (Exception e) {
            throw new ServiceException(HttpStatus.UNAUTHORIZED, "获取用户信息异常");
        }
    }

    /**
     * 用户ID
     **/
    public static Long getUserId() {
        try {
            return getLoginUser().getUserId();
        } catch (Exception e) {
            throw new ServiceException(HttpStatus.UNAUTHORIZED, "获取用户ID异常");
        }
    }

    /**
     * 获取部门ID
     **/
    public static Long getDeptId() {
        try {
            return getLoginUser().getDeptId();
        } catch (Exception e) {
            throw new ServiceException(HttpStatus.UNAUTHORIZED, "获取部门ID异常");
        }
    }

    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        try {
            return getLoginUser().getUsername();
        } catch (Exception e) {
            throw new ServiceException(HttpStatus.UNAUTHORIZED, "获取用户账户异常");
        }
    }


    /**
     * 获取登录IP地址
     **/
    public static String getIpaddr() {
        return getLoginUser().getIp();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        Sm3PasswordEncoder passwordEncoder = new Sm3PasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * userId是否为当前登录用户
     *
     * @param userId 用户ID
     * @return boolean
     */
    public static boolean isCurrentLogin(Long userId) {
        return getUserId().equals(userId);
    }
}
